package org.example.myleet.learn.counter;

import java.util.Random;
import java.util.concurrent.ForkJoinPool;

public class RecursiveTaskTest {

    public static void main(String[] args) {
        int size = 500000000;
        int[] numbers = new int[size];
        Random rand = new Random();
        for (int i = 0; i < size; ++i) {
            numbers[i] = rand.nextInt(size);
        }
        int result = 0;
        long start = System.currentTimeMillis();
        //count even number synchronously
        for (int i = 0; i < size; ++i) {
            if ((numbers[i] & 1) == 0) {
                ++result;
            }
        }
        System.out.printf("Even number synchronous count\t%d, time\t%dms\n", result, System.currentTimeMillis() - start);

        //count even number in multitask asynchronously
        start = System.currentTimeMillis();
        EvenCounter counter = new EvenCounter(numbers, 0, size);
        ForkJoinPool pool = new ForkJoinPool();
        pool.invoke(counter);
        result = counter.join();
        System.out.printf("Even number asynchronous count\t%d, time\t%dms\n", result, System.currentTimeMillis() - start);
    }
}
